package com.example.hms.service.servicelmpl;

import com.example.hms.dao.RoomTypeDao;
import com.example.hms.entity.RoomType;
import com.example.hms.entity.SystemLog;
import com.example.hms.entity.User;
import com.example.hms.service.RoomTypeService;
import com.example.hms.service.SystemLogService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class RoomTypeServiceImpl implements RoomTypeService {
    @Resource
    RoomTypeDao roomTypeDao;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Resource
    SystemLogService systemLogService;

    @Override
    public Result selectAllRoomType(String roomTypeName, BigDecimal roomPrice) {
        List<RoomType> rooms = roomTypeDao.selectAllRoomType(roomTypeName, roomPrice,null);
        return ResultSubmit.success(rooms);
    }

    @Override
    public Result insert(RoomType roomType, HttpServletRequest request) {
        roomType.setDataUpdateTime(format.format(new Date()));
        roomType.setRoomTypeId(Math.abs((int)System.currentTimeMillis()));
        roomType.setDeleteIdentity("未删除");
        int insert=roomTypeDao.insert(roomType);

        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("roomType");
//        systemLog.setUseTableDataIdBefore(String.valueOf(user.getUserTableId()));

        ;

        systemLog.setUseTableDataIdAfter(String.valueOf(
                roomTypeDao.selectAllRoomType(roomType.getRoomTypeName(), roomType.getRoomPrice(),null).
                        get(0).getRoomTypeTableId()));
        systemLog.setUseType("房间类型新增");
        systemLogService.insert(systemLog);

        if(insert==0){
            return ResultSubmit.error("添加失败");

        }else {
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.success("添加成功");
        }
    }

    @Override
    public Result update(RoomType roomType, HttpServletRequest request) {
        //    修改传入数据中的基础信息（删除标识、创建时间）
        int update1=roomTypeDao.update(roomType.getRoomTypeId().toString());
        roomType.setDeleteIdentity("未删除");
        roomType.setDataUpdateTime(format.format(new Date()));
        //    清除roomType中的基础信息
//        roomType.setRoomTypeId(0);
        int insert1=roomTypeDao.insert(roomType);

        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("roomType");
//        systemLog.setUseTableDataIdBefore(String.valueOf(user.getUserTableId()));

        systemLog.setUseTableDataIdBefore(String.valueOf(
                roomTypeDao.selectAllRoomType(roomType.getRoomTypeName(), roomType.getRoomPrice(),null).
                        get(0).getRoomTypeTableId()));
        ;
        systemLog.setUseTableDataIdAfter(String.valueOf(
                roomTypeDao.selectAllRoomType(roomType.getRoomTypeName(), roomType.getRoomPrice(),null).
                        get(0).getRoomTypeTableId()));
        systemLog.setUseType("房间类型更新");
        systemLogService.insert(systemLog);
        //    todo 系统记录操作
        //    根据删除方法、新增方法返回值判断逻辑修改整个功能是否执行成功
        //    不成功则返回报错并回滚
        if(update1==1 || insert1==1){
            return ResultSubmit.success("修改成功");
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("修改失败");
        }
    }

    @Override
    public Result delete(String roomTypeId, HttpServletRequest request) {
        Integer tableId = roomTypeDao.selectAllRoomType(null,null,
                roomTypeId).get(0).getRoomTypeTableId();

        int delete1=roomTypeDao.update(roomTypeId);

        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("roomType");
        systemLog.setUseTableDataIdBefore(String.valueOf(tableId));

//        systemLog.setUseTableDataIdBefore(String.valueOf(
//                roomTypeDao.selectAllRoomType(roomType.getRoomTypeName(), roomType.getRoomPrice()).
//                        get(0).getRoomTypeTableId()));
        ;
//        systemLog.setUseTableDataIdAfter(String.valueOf(
//                roomTypeDao.selectAllRoomType(roomType.getRoomTypeName(), roomType.getRoomPrice()).
//                        get(0).getRoomTypeTableId()));
        systemLog.setUseType("房间类型删除");
        systemLogService.insert(systemLog);


        if(delete1 !=0){
            return ResultSubmit.success("成功");
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("失败");
        }
    }

}
